package com.yipei.business.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlFlashNews;
import com.yipei.business.mapper.MlFlashNewsMapper;
import com.yipei.business.service.IMlFlashNewsService;
import com.yipei.common.utils.DateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 新闻快讯Service业务层处理
 *
 * @author yipei
 * @date 2024-12-21
 */
@Service
public class MlFlashNewsServiceImpl extends ServiceImpl<MlFlashNewsMapper, MlFlashNews> implements IMlFlashNewsService
{
    @Autowired
    private MlFlashNewsMapper mlFlashNewsMapper;

    /**
     * 查询新闻快讯
     *
     * @param id 新闻快讯主键
     * @return 新闻快讯
     */
    @Override
    public MlFlashNews selectMlFlashNewsById(Long id)
    {
        return mlFlashNewsMapper.selectMlFlashNewsById(id);
    }

    /**
     * 查询新闻快讯列表
     *
     * @param mlFlashNews 新闻快讯
     * @return 新闻快讯
     */
    @Override
    public List<MlFlashNews> selectMlFlashNewsList(MlFlashNews mlFlashNews)
    {
        return mlFlashNewsMapper.selectMlFlashNewsList(mlFlashNews);
    }

    /**
     * 新增新闻快讯
     *
     * @param mlFlashNews 新闻快讯
     * @return 结果
     */
    @Override
    public int insertMlFlashNews(MlFlashNews mlFlashNews)
    {
        mlFlashNews.setCreateTime(DateUtils.getNowDate());
        return mlFlashNewsMapper.insertMlFlashNews(mlFlashNews);
    }

    /**
     * 修改新闻快讯
     *
     * @param mlFlashNews 新闻快讯
     * @return 结果
     */
    @Override
    public int updateMlFlashNews(MlFlashNews mlFlashNews)
    {
        mlFlashNews.setUpdateTime(DateUtils.getNowDate());
        return mlFlashNewsMapper.updateMlFlashNews(mlFlashNews);
    }

    /**
     * 批量删除新闻快讯
     *
     * @param ids 需要删除的新闻快讯主键
     * @return 结果
     */
    @Override
    public int deleteMlFlashNewsByIds(Long[] ids)
    {
        return mlFlashNewsMapper.deleteMlFlashNewsByIds(ids);
    }

    /**
     * 删除新闻快讯信息
     *
     * @param id 新闻快讯主键
     * @return 结果
     */
    @Override
    public int deleteMlFlashNewsById(Long id)
    {
        return mlFlashNewsMapper.deleteMlFlashNewsById(id);
    }

    @Override
    public void addFlashNews() throws IOException {
        HttpClient client = HttpClients.createDefault();
        HttpGet request = new HttpGet("https://newsapi.eastmoney.com/kuaixun/v2/api/list?callback=ajaxResult_102"); //示例URL，请替换为实际的股票代码
        request.setHeader("Content-Type", "text/javascript;charset=UTF-8");
        HttpResponse response = client.execute(request);
        List<MlFlashNews> list= new ArrayList<>();
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8").replace("ajaxResult_102","");
            result = Stream.of(result.split(";")).collect(Collectors.joining(";"));
            result = result.substring(1, result.length() - 1);
            JSONObject jsonObject =  JSONObject.parseObject(result);
            JSONArray list1 = jsonObject.getJSONArray("news");
            list = list1.toJavaList(MlFlashNews.class).subList(0,20);
            for (MlFlashNews flashNews : list) {
                flashNews.setContent(flashNews.getDigest());
                flashNews.setPublishTime(flashNews.getShowtime());
                flashNews.setProvider(flashNews.getArt_Media_Name());
                flashNews.setThirdUrl(flashNews.getUrl_m());
            }
        }
        for (MlFlashNews mlFlashNews : list) {
            this.insertMlFlashNews(mlFlashNews);
        }
    }
}
